package io.coderyeah.pet.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.coderyeah.basic.Result;
import io.coderyeah.pet.domain.Pet;
import io.coderyeah.pet.dto.PetDTO;
import io.coderyeah.pet.service.PetService;
import io.coderyeah.system.domain.Menu;
import io.coderyeah.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2022-09-20 09:09:49
 */
@RestController
@RequestMapping("/pet")
@Api(tags = "宠物管理接口")
public class PetController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;
    @Resource
    private PetService petService;

    @ApiOperation("分页查询所有宠物以及父级菜单")
    @PostMapping("list")
    public Result getAllMenus(@RequestBody PetDTO petDTO) {
        return petService.getAllPets(petDTO);
    }

    @ApiOperation("修改状态")
    @PostMapping("/editType")
    public Result editType(@RequestBody PetDTO petDTO) {
        return petService.editType(petDTO);
    }

    @ApiOperation("添加/修改菜单")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Pet pet) {
        petService.saveOrModify(pet);
        return Result.success(null);
    }

    @ApiOperation("获取宠物类型树")
    @GetMapping("/tree")
    public Result getPetTypeTree() {
        return petService.getPetTypeTree();
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param menu 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Menu> page, Menu menu) {
        return success(this.menuService.page(page, new QueryWrapper<>(menu)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Long id) {
        return petService.getPetById(id);
    }

    /**
     * 新增数据
     *
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Menu menu) {
        return success(this.menuService.save(menu));
    }

    /**
     * 修改数据
     *
     * @param menu 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Menu menu) {
        return success(this.menuService.updateById(menu));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    public R delete(@PathVariable("id") String id) {
        return success(this.petService.removeById(id));
    }

    @PatchMapping("/delByBatch/{ids}")
    public R delByBatch(@PathVariable("ids") Long[] ids) {
        return success(this.petService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 宠物上架
     *
     * @param ids id数组
     * @return
     */
    @PostMapping("/onsale")
    public Result onsale(@RequestBody List<Long> ids) {
        return petService.onsale(ids);
    }

    @PostMapping("/offsale")
    public Result offsale(@RequestBody List<Long> ids) {
        return petService.offsale(ids);
    }
}

